<?php

namespace Game\Controller;

use Think\Controller;

class IndexController extends CommonController {

    public $gameid='1';

    //检测openid
    function checkopenid() {
        $openid = wx_openid();
        if (!$openid) {
            //$this->_empty();
        }
    }

    //404
    public function _empty() {
        header("HTTP/1.0 404 Not Found"); //使HTTP返回404状态码
        $this->display("Public:404");
        exit();
    }


    function index() {

        //$this->display("endup");
        //exit();

        $this->checkopenid();

        $openid = wx_openid();
        $rs = check_vip($openid); //会员信息

        //test
        //$openid = '1234567';
        //$rs['card_no']=1;

        $jumpurl="";
        if(!$rs) {
            //header("Location:http://a.forcent.cn/iHuiyuan/user/Integral?url=".urlencode(U("Index/index",'',true,true)));
            $jumpurl="http://a.forcent.cn/iHuiyuan/user/Integral?url=".urlencode(U("Index/index",'',true,true));
        }
        $this->assign("jumpurl",$jumpurl);

        $userscore=M("GameScore")->where("openid='".$openid."' and gameid='".$this->gameid."' and username!='' and mobile!='' ")->order("id desc ")->select();
        $username='';
        $mobile='';
        foreach($userscore as $s) {
            if($s['username'] && $s['mobile']) {
                $username=$s['username'];
                $mobile=$s['mobile'];
                break;
            }
        }

        $this->assign("username",$username);
        $this->assign("mobile",$mobile);
        $this->assign("is_member",$rs?1:0);
        $this->display();
    }

    public function ajaxAddScore(){
        $score=$_GET['score'];
        $openid = wx_openid();
        $rs = check_vip($openid); //会员信息

        //test
        //$openid = '1234567';
        //$rs['card_no']=1;

        $userscore=M("GameScore")->where("openid='".$openid."' and gameid='".$this->gameid."'")->order("id desc ")->select();
        $userscore=$userscore?$userscore[0]:false;
        /*if( trim($userscore['username'])=='' || trim($userscore['mobile'])=='') {
            $result['is_addressed']=0;
        }else{
            $result['is_addressed']=1;
        }*/
        $data=array();
        $data['openid']=$openid;
        $data['gameid']=$this->gameid;
        $data['score']=$score;
        $data['is_member']=$rs?1:0;
        $data['ctime']=date("Y-m-d H:i:s");
        $result['is_addressed']=0;
        if($rs) {
            $data['username']=$rs['huiyuan_name'];
            $data['mobile']=$rs['huiyuan_tel'];
            $result['is_addressed']=1;
        }
        $result['is_addscore']=0;
        if($rs && (!$userscore || ($userscore && date("Y-m-d")!=date("Y-m-d",strtotime($userscore['ctime']))))) {
            $data['score_member']=100;
            //添加积分
            if(!addCredits($rs['card_no'], $data['score_member'])) {
                addCredits($rs['card_no'], $data['score_member']);
            };
            $result['is_addscore']=1;
        }
        //$result['is_addscore']=1;
        $result['success']=M("GameScore")->add($data);
        $result['is_addressed']=1;
        echo json_encode($result);
        exit(0);
    }

    public function ajaxAddUserinfo(){
        $openid = wx_openid();

        //test
        //$openid = '1234567';

        $userscore=M("GameScore")->where("openid='".$openid."' and gameid='".$this->gameid."'")->order("id desc ")->select();
        $userscore=$userscore?$userscore[0]:false;
        $data=array();
        $data['username']=$_GET['username'];
        $data['mobile']=$_GET['mobile'];
        if(!$data['username'] || !$data['mobile']) {
            $result=0;
        }else{
            if($userscore) {
                $result=M("GameScore")->where("id='".$userscore['id']."'")->save($data);
            }else{
                $result=0;
            }
        }
        echo json_encode($result);
        exit(0);
    }

    public function ajaxGetRank2(){
        $openid = wx_openid();
        $rs = check_vip($openid); //会员信息

        //test
        //$openid = '1234567';
        //$rs['card_no']=1;

        $search=" and is_member=0";
        if($rs) {
            //会员
            $search=" and is_member=1";
        }
        $search.=" and ctime > '".$this->last_friday()."'";

        $num=$_GET['num']?$_GET['num']:20;
        $userscores=M("GameScore")
            ->field("max(score) as score,username,mobile")
            ->where(" gameid='".$this->gameid."' and username!='' ".$search)
            ->order("score desc")
            ->group("username")
            ->limit("0,".$num)
            ->select();
        echo json_encode($userscores);
        exit(0);
    }

    public function ajaxGetRank(){
        $openid = wx_openid();
        $rs = check_vip($openid); //会员信息

        //test
        //$openid = '1234567';
        //$rs['card_no']=1;

        $search=" and is_member=0";
        if($rs) {
            //会员
            $search=" and is_member=1";
        }
        //$search.=" and ctime > '".$this->last_friday()."'";

        //$num=$_GET['num']?$_GET['num']:20;
        $userscores=M("GameScore")
            ->field("score,username,mobile,DATE_FORMAT( ctime, '%Y-%m-%d %H:%i' ) as ctime")
            ->where(" gameid='".$this->gameid."' and username='".$rs['huiyuan_name']."' ".$search)
            ->order("score desc")
           // ->limit("0,".$num)
            ->select();
        echo json_encode($userscores);
        exit(0);
    }


    public function ajaxGetMyRank(){
        $openid = wx_openid();

        //test
        //$openid = '1234567';

        $num=$_GET['num']?$_GET['num']:20;
        $userscores=M("GameScore")
            ->where(" gameid='".$this->gameid."'  and openid='".$openid."'  and score_member!=0 ")
            ->order("ctime desc")
            ->limit("0,".$num)
            ->select();
        foreach($userscores as $key=>$val) {
            $val['ctime']=date("m-d H:i:s",strtotime($val['ctime']));
            $userscores[$key]=$val;
        }
        echo json_encode($userscores);
        exit(0);
    }


    function last_friday(){
        $timestamp = time();
        $time= $timestamp-24*3600*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-6*86400);
        $friday = $time-4*86400;
        $date = date('Y-m-d',$friday);
        return $date." 00:00:00";
    }

}
